package net.adst.ui.mainframe.tabs.ourTab;

import net.adst.beans.OurTecInf;
import net.adst.beans.OurTecRow;
import net.adst.resources.Constants;
import net.adst.resources.VariableNames;
import net.adst.util.DataManager;
import net.adst.util.GBC;
import net.adst.util.OurTecCellRender;

import javax.swing.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import java.awt.*;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;

/**
 * Created with IntelliJ IDEA.
 * User: alekseipot
 * Date: 02.11.12
 * Time: 6:57
 * To change this template use File | Settings | File Templates.
 */
public class DetailedTableOurPanel extends JPanel {
    private JTable detailedTable;

    public DetailedTableOurPanel() {
        setLayout(new GridBagLayout());
        addDetTable();
    }

    private void addDetTable() {
        DefaultTableModel model = new DefaultTableModel( //NotEditableDefaultTableModel
                new Object[][]{
                },
                Constants.OUR_НEADER
        ) {
            @Override
            public boolean isCellEditable(final int row, final int col) {
                if (col == 1 || col == 8 || col == 12 || col == 13 || col == 14 || col == 17 || col == 20) {
                    return false;
                }
                return true;
            }
        };
        detailedTable = new JTable(model);
        detailedTable.setRowHeight(Constants.TABLE_ROW_HEIGHT);
        detailedTable.getTableHeader().setReorderingAllowed(false);
        detailedTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {
                if (e.getValueIsAdjusting())
                    return;
                ListSelectionModel lsm = (ListSelectionModel) e.getSource();

                final JTextArea description = (JTextArea) DataManager.getObject(VariableNames.COMMENT_TEXT);

                if (lsm.isSelectionEmpty()) {
                    description.setText("");
                    description.setEnabled(false);
                    description.setEditable(false);
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(false);
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_OUR_BUTTON)).setEnabled(false);
                } else {
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_REMOVE_BUTTON)).setEnabled(true);
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_MARK_OUR_BUTTON)).setEnabled(true);
                    final JTable sumTable = (JTable) DataManager.getObject(VariableNames.UI_OUR_SUM_TABLE);
                    sumTable.getSelectionModel().clearSelection();
                    final int selectedRow = lsm.getMinSelectionIndex();
                    DataManager.setObject(VariableNames.OUR_SELECTED_ROW, (Integer) selectedRow);

                    int col = detailedTable.getColumnModel().getColumnIndex(Constants.OUR_НEADER[20]);
                    if (selectedRow > -1) {
                        if (detailedTable.getValueAt(selectedRow, col).equals("*")) {
                            ArrayList<OurTecRow> rows = (ArrayList<OurTecRow>) DataManager.getObject(VariableNames.TEC_OUR_USING_ROWS);
                            description.setText(rows.get(selectedRow).getComment());
                            description.setEnabled(true);
                            description.setEditable(true);
                        } else {
                            description.setEnabled(true);
                            description.setEditable(true);
                            description.setText("");
                        }
                    } else {
                        return;
                    }
                }
            }
        });
        detailedTable.setDefaultRenderer(Object.class, new OurTecCellRender());
        model.addTableModelListener(new TableModelListener() {
            @Override
            public void tableChanged(TableModelEvent e) {
                if (e.getType() != 1 && e.getType() != -1) {
                    int row = e.getFirstRow();
                    JButton editButton = (JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_SAVE_BUTTON);
                    editButton.setEnabled(true);
                    OurTecInf tec = new OurTecInf("", "", "", "", "");
                    ArrayList<OurTecInf> tecInf = (ArrayList<OurTecInf>) DataManager.getObject(VariableNames.TEC_OUR_INFO);
                    String shortName = detailedTable.getValueAt(row, 1).toString();
                    for (OurTecInf item : tecInf) {
                        if (item.getShortName().equals(shortName)) {
                            tec = new OurTecInf(item.getFullName(), item.getShortName(), item.getCostHour(), item.getRashod(), item.getSalary());
                        }
                    }

                    if (e.getColumn() == 5 && !detailedTable.getValueAt(row, 5).toString().isEmpty()) {
                        try {
                            detailedTable.setValueAt(new BigDecimal(Double.valueOf(detailedTable.getValueAt(row, 5).toString()) * Double.valueOf(tec.getRashod())).setScale(2, RoundingMode.HALF_DOWN).toString(), row, 12);
                            detailedTable.setValueAt(new BigDecimal(Double.valueOf(detailedTable.getValueAt(row, 12).toString()) * Double.valueOf(DataManager.getObject(VariableNames.TOPLIVO_COST).toString())).setScale(2, RoundingMode.HALF_DOWN).toString(), row, 13);
                            detailedTable.setValueAt(new BigDecimal(Double.valueOf(detailedTable.getValueAt(row, 5).toString()) * Double.valueOf(tec.getSalary())).setScale(2, RoundingMode.HALF_DOWN).toString(), row, 14);
                        } catch (IllegalArgumentException ex) {
                            detailedTable.setValueAt("", row, 5);
                        }
                    }
                    if (e.getColumn() == 6 && !detailedTable.getValueAt(row, 6).toString().isEmpty()) {
                        try {
                            detailedTable.setValueAt(Double.valueOf(detailedTable.getValueAt(row, 6).toString()) * Double.valueOf(detailedTable.getValueAt(row, 7).toString()), row, 8);
                        } catch (IllegalArgumentException ex) {
                            detailedTable.setValueAt("", row, 6);
                        }
                    }
                    if (e.getColumn() == 5 || e.getColumn() == 6 || e.getColumn() == 15 || e.getColumn() == 18 || e.getColumn() == 19) {
                        Double pribl = !detailedTable.getValueAt(row, 8).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 8).toString())) : new Double("0");
                        Double dtRub = !detailedTable.getValueAt(row, 13).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 13).toString())) : new Double("0");
                        Double salary = !detailedTable.getValueAt(row, 14).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 14).toString())) : new Double("0");
                        Double dop = new Double("0");
                        Double arenda = new Double("0");
                        Double prorab = new Double("0");
                        try {
                            dop = !detailedTable.getValueAt(row, 15).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 15).toString())) : new Double("0");
                        } catch (IllegalArgumentException ex) {
                            detailedTable.setValueAt("", row, 15);
                        }
                        try {
                            arenda = !detailedTable.getValueAt(row, 18).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 18).toString())) : new Double("0");
                        } catch (IllegalArgumentException ex) {
                            detailedTable.setValueAt("", row, 18);
                        }
                        try {
                            prorab = !detailedTable.getValueAt(row, 19).toString().isEmpty() ? Double.valueOf(Double.valueOf(detailedTable.getValueAt(row, 19).toString())) : new Double("0");
                        } catch (IllegalArgumentException ex) {
                            detailedTable.setValueAt("", row, 19);
                        }
                        detailedTable.setValueAt(pribl * 0.95 - dtRub - salary - dop - arenda - prorab, row, 17);
                    }

                }
            }
        }
        );
        detailedTable.addPropertyChangeListener(new PropertyChangeListener() {
            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                if (evt.getPropertyName().equals("tableCellEditor") && evt.getNewValue() != null) {
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_SAVE_BUTTON)).setEnabled(false);
                }
                if (evt.getPropertyName().equals("tableCellEditor") && evt.getNewValue() == null) {
                    ((JButton) DataManager.getObject(VariableNames.UI_TOP_PANEL_SAVE_BUTTON)).setEnabled(true);
                }
            }
        });

//        setWidth();
        JScrollPane scrollPane = new JScrollPane(detailedTable);
        DataManager.setObject(VariableNames.UI_OUR_DETAILED_MODEL, model);
        DataManager.setObject(VariableNames.UI_OUR_DETAILED_TABLE, detailedTable);
        detailedTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        add(scrollPane, new GBC(0, 0).setFill(GBC.BOTH).setWeight(1, 1));
    }

    private void setWidth() {
        // ORS-22
        int col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[0]);
        TableColumn column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[1]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(55);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[2]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(60);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[3]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(150);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[4]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[5]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(160);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[6]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(80);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[7]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(80);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[8]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[9]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(110);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[10]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[11]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[12]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[13]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[14]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(120);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[15]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);

        col = detailedTable.getColumnModel().getColumnIndex(Constants.NAEM_НEADER[16]);
        column = detailedTable.getColumnModel().getColumn(col);
        column.setPreferredWidth(70);
    }


}
